<?php

/**
 * @filename Recharge.php 
 * @encoding UTF-8 
 * @author 徐小军
 * @link http://www.okd100.com
 * @copyright 南昌欧科达科技有效公司 
 * @datetime 2016-8-3  13:23:28
 * @version 1.0
 * @Description
 */

namespace app\admin\validate;

use think\Validate;

class Recharge extends Validate {

    protected $rule = [
        'user_uniid|充值会员编号' => 'require|length:12|checkUserName:thinkphp',
        'cash_type|充值类型' => 'require|checkUserType:thinkphp',
        'cash_value|充值金额' => 'require|number|checkUserMoney:thinkphp',
    ];
    protected $scene = [
        'add' => ['user_uniid', 'cash_type', 'cash_value']
    ];

    // 验证用户名信息
    protected function checkUserName($value, $rule, $data) {
        $user = \app\common\model\User::where('user_uniid', $value)->find();
        if (!$user) {
            return "该会员编号不存在";
        }

        if ($user->user_status == "未认证" || $user->user_status == "已删除") {
            return $user->user_status . "用户无法充值";
        }
        return true;
    }

    //验证用户身份
    protected function checkUserType($value, $rule, $data) {
        if ($value == "报单币") {
            $user_uniid = $data['user_uniid'];
            $user = \app\common\model\User::where('user_uniid', $user_uniid)->find();
            if ($user->user_type != "agent") {
                return "非代理身份无法充值报单币";
            }
        }
        return true;
    }

    //验证用户币种不能为负
    protected function checkUserMoney($value, $rule, $data) {
        $user_uniid = $data['user_uniid'];
        $cash_type = $data['cash_type'];
        $user = \app\common\model\User::where('user_uniid', $user_uniid)->find();

        if ($cash_type == "财富币") {
            $user_money = $user->money_cash;
            if ($data['cash_value'] < 0 && ($user_money < abs($data['cash_value']))) {
                return "该用户财富币为" . $user_money . "不够本次扣除。";
            }
        } elseif ($cash_type == "报单币") {
            $user_money = $user->line_cash;
            if ($data['cash_value'] < 0 && ($user_money < abs($data['cash_value']))) {
                return "该用户报单币为" . $user_money . "不够本次扣除。";
            }
        }
        return true;
    }

}
